'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 邮件
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');

///////////////////////////////////////////////////////////
/**
 * 附件属性
 * @type {mongoose}
 */
var attachSchema = new mongoose.Schema({
    //奖励
    reward: {
        type: Number,
        default: 0
    },

    //奖励类型
    reward_type: {
        type: Number,
        default: 0
    },

    // 奖励数量
    reward_num: {
        type: Number,
        default: 0
    },

    // 奖励值
    reward_value: {
        type: Number,
        default: 0
    },

    // 孔
    hole_num: {
        type: Number,
        default: 0
    }
});

if (!attachSchema.options.toJSON) {
    attachSchema.options.toJSON = {};
}

/* jshint unused:false */
attachSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

///////////////////////////////////////////////////////////
/**
 * 子邮件
 * @type {mongoose}
 */
var mailItemSchema = new mongoose.Schema({
    // 邮件编号
    mail_id: {
        type: Number,
        require: true,
        index: true
    },

    mail_type: {
        type: Number,
        default: 1
    },

    // 标题
    title: {
        type: String,
        default: ''
    },

    // 内容
    content: {
        type: String,
        default: ''
    },

    // 附件
    attach_list: [attachSchema],

    // 是否已读
    is_read: {
        type: Boolean,
        default: false
    },

    // 邮件时间
    create_time: {
        type: Date,
        default: Date.now
    },

    // 来源模块
    module_type: {
        type: Number,
        default: 1
    },

    // 操作类型
    operation_type: {
        type: Number,
        default: 1
    },

    // 附加信息(整数)
    reserve_int_val: {
        type: Number,
        default: 1
    },

    reserve_str_val: {
        type: String,
        default: ''
    },
});

// 获取邮件附件
mailItemSchema.methods.getAttach = function () {
    return this.attach_list;
};

// 删除邮件奖励
mailItemSchema.methods.deleteAttach = function () {
    this.attach_list = [];
};

// 获取并删除邮件附件
mailItemSchema.methods.getAndRemoveAttach = function () {
    var reward_list = this.attach_list;
    this.attach_list = [];
    return reward_list;
};

// 获取邮件状态
mailItemSchema.methods.getReadStatus = function () {
    return this.is_read;
};

// 设置邮件状态
mailItemSchema.methods.setReadStatus = function () {
    this.is_read = true;
};

if (!mailItemSchema.options.toJSON) {
    mailItemSchema.options.toJSON = {};
}
/* jshint unused:false */
mailItemSchema.options.toJSON.transform = function (doc, ret) {
    ret.create_time = moment(ret.create_time).unix();
    if (ret.is_read !== undefined) {
        ret.is_read = +ret.is_read;
    }

    delete ret._id;
    delete ret.__v;
};

module.exports = mailItemSchema;
